<template>
	<uni-popup ref="popup" type="bottom" @change="change">
		<view class="popup-content d-flex flex-column">
			<view class="d-flex justify-content-end">
				<image src="/static/images/common/clousex-big.png" class="close_btn" @tap="close"></image>
			</view>
			<view class="d-flex flex-fill flex-column justify-content-between">
				<view class="d-flex flex-column">
					<view class="font-size-extra-lg font-weight-bold text-color-base mb-30">欢迎登陆喜茶星球~</view>
					<view class="font-size-base text-color-assist">登录后消费可获取积分，享受更好的服务体验</view>
				</view>
				<view class="d-flex flex-column">
					<form>
						<view class="input-group">
							<view class="input">
								<input v-model="form.LoginName" type="text" placeholder="用户名/手机号" />
							</view>
							<view class="input">
								<input v-model="form.LoginPwd" type="text" placeholder="密码" />
							</view>
						</view>

						<button type="primary" class="w-100 font-size-lg mb-30" open-type="getUserInfo"
							@click="getUserInfo">登陆</button>
					</form>
					<view class="text-center mb-30 font-size-sm text-color-assist">
						点击登陆喜茶GO，即表示已阅读并同意<font class="text-color-primary">《喜茶隐私政策》</font>
					</view>
					<!-- <view class="text-center font-primary font-size-sm text-color-primary">《喜茶GO小程序服务指南》</view> -->
				</view>
			</view>
		</view>
	</uni-popup>
</template>

<script>
	import uniPopup from '@/components/uni-popup/uni-popup.vue'
	import login from '@/api/login.js'
	import {
		mapMutations,
	} from 'vuex'

	export default {
		name: 'LoginPopup',
		components: {
			uniPopup
		},
		props: {

		},
		data() {
			return {
				form: {
					LoginName: '',
					LoginPwd: ''
				}
			}
		},
		methods: {
			...mapMutations("user", ['SET_USERINFO', 'SET_ISLOGIN']),
			open() {
				this.$refs['popup'].open()
			},
			close() {
				this.$refs['popup'].close()
			},
			change({
				show
			}) {
				this.$emit('change', show)
			},
			async getUserInfo(e) {
				let res = await login.login(this.form)
				if (res.code == 1) {
					this.SET_USERINFO(res.data.user)
					console.log(res);
					this.SET_ISLOGIN(true)
					sessionStorage.setItem('Token', res.data.jwt)
					this.close()
				} else {
					uni.showModal({
						content: res.msg,
						showCancel: false
					})
				}
			}
		}
	}
</script>

<style lang="scss" scoped>
	.input-group {
		display: flex;
		flex-direction: column;
		row-gap: 0.5rem;
		margin: 0.5rem 0;

		.input {
			position: relative;

			input {
				height: 32px;
				background-color: #f7f7f7;
				padding: 2px 4px;
			}
		}
	}

	.popup-content {
		background-color: #FFFFFF;
		border-radius: 24rpx 24rpx 0 0;
		height: 50vh;
		padding: 50rpx 40rpx;
	}

	.close_btn {
		width: 40rpx;
		height: 40rpx;
	}
</style>